package com.gba.mark.exam.controller;



import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gba.mark.exam.entity.ExamPaper;
import com.gba.mark.exam.entity.ExamQuestions;
import com.gba.mark.exam.service.ExamQuestionsService;
import com.gba.mark.util.ResponseUtil;
import com.qigetech.utils.resultbundle.ResultBundle;
import com.qigetech.utils.resultbundle.ResultBundleBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.spring.web.json.Json;

import java.util.List;

@RestController
@RequestMapping("/exam/questions")
public class ExamQuestionsController {

    @Autowired
    private ExamQuestionsService examQuestionsService;

    @Autowired
    private ResultBundleBuilder resultBundleBuilder;


    /**
     * 新建并保存一份题目
     */
    @PostMapping("/saveQuestion")
    public ResponseUtil<Integer> update(@RequestBody ExamQuestions examQuestions) {
        return new ResponseUtil<>(HttpStatus.OK, examQuestionsService.saveQuestion(examQuestions));
    }


//    /*
//   从前端接收List保存并更新一份题目
//     */
//
//    @PostMapping("/receiveData")
//    public ResponseUtil<List<ExamQuestions>> receiveData(
//            @RequestParam List<ExamQuestions> list) {
//
//        return new ResponseUtil<>(HttpStatus.OK,
//                examQuestionsService.receiveData(list));
//}

    /*
    删除一个题目
     */

    @DeleteMapping("/{id}")
    public ResultBundle<Boolean> delete(@PathVariable("id") int id) {
        return resultBundleBuilder.bundle("", () -> examQuestionsService.removeById(id));
    }

    @GetMapping("/{id}")
    public ResultBundle<ExamQuestions> queryById(@PathVariable("id") int id) {
        return resultBundleBuilder.bundle("", () -> examQuestionsService.getById(id));
    }

    @GetMapping("/list")
    public ResultBundle<IPage<ExamQuestions>> list(@RequestParam(name = "pageNum", defaultValue = "0") int pageNum,
                                                   @RequestParam(name = "pageSize", defaultValue = "20") int pageSize) {// 1 10
        return resultBundleBuilder.bundle("", () ->
                examQuestionsService.page(new Page<>(pageNum, pageSize)));
    }


    /*
    查询一个题目
     */

    @PostMapping("/findQuestion")
    public ResponseUtil<List<ExamQuestions>> findQuestion(@RequestParam String questions,
                                                          @RequestParam Integer group_id,
                                                          @RequestParam Json question_ids) {
        return new ResponseUtil<>(HttpStatus.OK,
                examQuestionsService.findQuestion(questions, group_id, question_ids));


    }
}
